// import signMd5Utils from '@/utils/encryption/signMd5Utils'
import request from "@/utils/request";
import {Loading} from "element-ui";
import Decimal from 'decimal.js';

//查询方法
export function getAction(url, parameter) {
  // let sign = signMd5Utils.getSign(url, parameter);
  // 将签名和时间戳，添加在请求接口 Header
  // let signHeader = {"X-Sign": sign,"X-TIMESTAMP": signMd5Utils.getDateTimeToString()};
  return request({
    url: url,
    method: 'get',
    params: parameter,
    // headers: signHeader
  })
}

//查询方法
export function postAction(url, parameter) {
  // let sign = signMd5Utils.getSign(url, parameter);
  // 将签名和时间戳，添加在请求接口 Header
  // let signHeader = {"X-Sign": sign,"X-TIMESTAMP": signMd5Utils.getDateTimeToString()};
  return request({
    url: url,
    method: 'post',
    params: parameter,
    // headers: signHeader
  })
}


// 根据ID查询
export function getById(url, id) {
  return request({
    url: url + id,
    method: 'get'
  })
}

// 新增方法
export function addAction(url, data) {
  return request({
    url: url,
    method: 'post',
    data: data
  })
}

// 修改事件方法
export function updateAction(url, data) {
  return request({
    url: url,
    method: 'put',
    data: data
  })
}

//删除方法
export function deleteAction(url, id) {
  return request({
    url: url + id,
    method: 'delete',
  })
}


// 导出方法
export function exportAction(url, parameter) {
  return request({
    url: url,
    method: 'get',
    params: parameter
  })
}

// 导出方法
export function deepClone(obj) {
  return JSON.parse(JSON.stringify(obj));
}


/**
 * 封装的导出文件的方法
 * @param url
 * @param parameter
 * @param pageSizeZero 是否全部导出
 * @returns {Promise<AxiosResponse<any>>}
 */
export function downloadFile(url, parameter, pageSizeZero){
  const loading = Loading.service({
    lock: true,
    text: '下载中，请稍后',
    spinner: 'el-icon-loading',
  });
  return request({
    url: url,
    params: {...parameter, pageSizeZero : (pageSizeZero !== undefined ? pageSizeZero : true)},
    method: 'get',
    responseType: 'blob',
    timeout: 30000,
  }).then((data) => {
    let fileName = decodeURIComponent(data.fileName)
    if (!data.data) {
      const message = '文件下载失败';
      return Promise.reject(new Error(message))
    }
    if (typeof window.navigator.msSaveBlob !== 'undefined') {
      window.navigator.msSaveBlob(new Blob([data.data]), fileName)
    }else{
      let url = window.URL.createObjectURL(new Blob([data.data]))
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', fileName)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    }
  }).finally(()=>{
    loading.close();
  })
}




/**
 * @param {*} arg1 数字
 * @param {*} arg2 数字
 * @description 两数字相加 保持精度
 */
export const accAdd = (arg1, arg2) => {
  let a = new Decimal(arg1);
  let b = new Decimal(arg2);
// 进行加法运算
  let sum = a.plus(b);
  return sum.toString()
}


/**
 * @param {*} arg1 数字
 * @param {*} arg2 数字
 * @description 两数字相减 保持精度
 */
export const accSub = (arg1, arg2) => {
  let r1, r2, m, n
  try { r1 = arg1.toString().split('.')[1].length } catch (e) { r1 = 0 }
  try { r2 = arg2.toString().split('.')[1].length } catch (e) { r2 = 0 }
  m = Math.pow(10, Math.max(r1, r2))
  n = (r1 >= r2) ? r1 : r2
  return ((arg1 * m - arg2 * m) / m).toFixed(n)
}


/**
 * @param {*} arg1 数字
 * @param {*} arg2 数字
 * @description 两数字相乘 保持精度
 */
export const accMul = (arg1, arg2) => {
  let m = 0
  const s1 = arg1.toString()
  const s2 = arg2.toString()
  try { m += s1.split('.')[1].length } catch (e) { }
  try { m += s2.split('.')[1].length } catch (e) { }
  return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
}


/**
 * @param {*} arg1 数字
 * @param {*} arg2 数字
 * @description 两数字相除 保持精度
 */
export const accDiv = (arg1, arg2) => {
  let t1 = 0
  let t2 = 0
  let r1, r2
  try { t1 = arg1.toString().split('.')[1].length } catch (e) { }
  try { t2 = arg2.toString().split('.')[1].length } catch (e) { }
  r1 = Number(arg1.toString().replace('.', ''))
  r2 = Number(arg2.toString().replace('.', ''))
  let intDiv = r1 / r2
  let pow = Math.pow(10, t2 - t1)
  return accMul(intDiv, pow) // 这里用上面定义好的乘法运算
}
// 复制内容
export function chsoeCopy(scope) {  
  const save = function (e) {
    e.clipboardData.setData("text/plain", scope);
    e.preventDefault();                     // 阻止默认行为
  };
  document.addEventListener("copy", save);  // 添加一个copy事件
  document.execCommand("copy");             // 执行copy方法
  // 复制成功提示  
  this.$message({ message: "复制成功", type: "success" });
}

export function isEmpty(data){
  if (typeof data === 'undefined' || data === null) {
    return true;
  }
  if (typeof data === 'string' && data.trim() === '') {
    return true;
  }
  if (typeof data === 'object' && Object.keys(data).length === 0) {
    return true;
  }
  if (Array.isArray(data) && data.length === 0) {
    return true;
  }
  return false;
}








